﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Text;

namespace CWebSocket
{
    public class HandlerHelper
    {
        private static Dictionary<string, JsEvent> eventDict = new Dictionary<string, JsEvent>();
        private static Dictionary<string, JsCallResultDelegate> returnDict = new Dictionary<string, JsCallResultDelegate>();

        public static void RegisterHandler(string namespaces, JsEvent events)
        {
            if (eventDict.ContainsKey(namespaces))
            {
                eventDict.Remove(namespaces);
            }
            eventDict.Add(namespaces, events);
        }

        public static void RegisterJsReturn(string method, JsCallResultDelegate call)
        {
            if (returnDict.ContainsKey(method))
            {
                returnDict.Remove(method);
            }
            returnDict.Add(method, call);
        }

        private static void RemoveJsReturn(string method)
        {
            if (returnDict.ContainsKey(method))
            {
                returnDict.Remove(method);
            }
        }

        public static void call(Socket client, Message mess)
        {
            try
            {
                string[] urls = mess.Url.Split('.');
                if (urls.Length == 1)
                {
                    if (returnDict.ContainsKey(mess.Url))
                    {
                        Console.WriteLine("返回数据的Js Method：" + mess.Url);
                        Console.WriteLine("Js Method 返回的数据：" + mess.Data);
                        JsCallResultDelegate callResult = returnDict[mess.Url];
                        callResult(mess.Data);
                        HandlerHelper.RemoveJsReturn(mess.Url);
                    }
                }
                else if (urls.Length >= 2)
                {
                    Console.WriteLine("客户端请求URL：" + mess.Url);
                    Console.WriteLine("客户端请求DATA：" + mess.Data);

                    string names = urls[0];
                    string methodName = urls[1];
                    if (eventDict.ContainsKey(names))
                    {
                        JsEvent events = eventDict[names];
                        Type t = events.GetType();
                        MethodInfo method = t.GetMethod(methodName);
                        if (method != null)
                        {
                            method.Invoke(events, new object[] { client, mess.Data });
                        }
                        else
                        {
                            Console.WriteLine("未实现的方法：" + method);
                        }
                    }
                    else
                    {
                        Console.WriteLine("未注册的命名空间：" + names); 
                    }
                }               
            }
            catch(Exception e)
            {
                Console.WriteLine("反射调用方法异常：" + e);
            }
            
        }

    }
}
